﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using System.Drawing.Imaging;
using CefSharp.WinForms;
using CefSharp;

namespace DC.Views
{
    public partial class Form_ViewExample : Skin_DevExpress
    {
        public string ExampleID = string.Empty;

        Utils.Database DB = Utils.Global.GlobalDB;
        public Form_ViewExample()
        {
            InitializeComponent();
            InitializeChromium();
        }

        private void InitializeChromium()
        {
            chromeBrowser = new ChromiumWebBrowser(Application.StartupPath + @"\Resources\Location\index.html");
            this.panel2.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        private void Form_ViewExample_Load(object sender, EventArgs e)
        {
            loadExampleDetails();
        }

        private ChromiumWebBrowser chromeBrowser;

        private void loadExampleDetails()
        {
            string sql = @"select [User].Real_Name,
                            Patient.Patient_Name,
                            Example_ID,
                            Example_Time,
                            Example_Main,
                            Example_Past,
                            Example_Allergic,
                            Example_Now,
                            Cure,
                            Advice,
                            Location,
                            IsFirst
                            from Example,[User],Patient where Example_ID='" + ExampleID + "' and Example.User_ID=[User].User_ID and Example.Patient_ID=Patient.Patient_ID";
            DataRow ExampleDetails = DB.GetDs(sql).Tables[0].Rows[0];
            chromeBrowser.LoadingStateChanged += (sender, args) =>
            {
                //Wait for the page to finish loading
                if (args.IsLoading == false)
                {
                    //chromeBrowser.ShowDevTools();
                    chromeBrowser.ExecuteScriptAsyncWhenPageLoaded("setLocations(\'" + ExampleDetails["Location"].ToString() + "\')");
                }
            };

            //chromeBrowser.ExecuteScriptAsync(@"setLocations(" + ExampleDetails["Location"].ToString() + ")");
            label31.Text = ExampleDetails["Real_Name"].ToString();
            label30.Text = ExampleDetails["Patient_Name"].ToString();
            label29.Text = ExampleDetails["Example_Time"].ToString();
            label16.Text = ExampleDetails["Example_Main"].ToString();
            label17.Text = ExampleDetails["Example_Past"].ToString();
            label18.Text = ExampleDetails["Example_Allergic"].ToString();
            label20.Text = ExampleDetails["Example_Now"].ToString();
            label21.Text = ExampleDetails["Cure"].ToString();
            label22.Text = ExampleDetails["Advice"].ToString();
            label15.Text = ExampleDetails["IsFirst"].ToString() == "0" ? "初诊" : "复诊";
        }
        private void skinButton1_Click(object sender, EventArgs e)
        {
            Bitmap image = Utils.ScreenCapture.GetWindowCapture(panel1.Handle);
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "JPG File|*.jpg";
            dialog.FileName = label30.Text + " - " + label29.Text.Replace(":", "") + ".jpg";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                image.Save(dialog.FileName, ImageFormat.Jpeg);
                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            PrintDialog MyPrintDg = new PrintDialog();
            MyPrintDg.Document = printDocument1;
            if (MyPrintDg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MessageBox.Show("1");
                    printDocument1.Print();
                }
                catch
                {   //停止打印
                    MessageBox.Show("2");
                    printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs());
                }
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            MessageBox.Show("3");

            Bitmap image = Utils.ScreenCapture.GetWindowCapture(panel1.Handle);
            e.Graphics.DrawImage(image, 0, 20);
        }
    }
}
